using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
//t
namespace FallenValkyrie
{
    class AI
    {
        Texture2D EnemyAITexture;
        Vector2 EnemyAIOri;
        Vector2 EnemyAIPos;
        Vector2 PlayerPos;
        float EnemyAIAttRng;
        float EnemyAISpd;

        float distance;
        float rotation = 0f;
        
        float timeDelay;
        float finalTimeDelay;
        bool isSet = false;

        int changeState;

        public AI (float enemySpeed , float enemyAttackRange) {
            EnemyAISpd = enemySpeed;
            EnemyAIAttRng = enemyAttackRange;
        }

        public void UpdateAI(Vector2 offSet, Vector2 EnemyAIPos, 
            ref Vector2 EnemySpeed, ref Vector2 EnemyDirection, 
            ref string State_AI, ref bool StateEnd, GameTime gt)
        {
            //aquire distance between 2 object
            distance = Vector2.Distance(offSet, EnemyAIPos);
            Random stateRandom = new Random();

            //when enemy is close enough to melee
            if (distance < EnemyAIAttRng)
            {
                State_AI = "idle";
                timeDelay += (float)gt.ElapsedGameTime.TotalSeconds;

                if (!isSet)
                {
                    changeState = stateRandom.Next(1, 3);
                    finalTimeDelay = FloatRandomBetween(0.5f, 3);
                    isSet = true;
                }

                //Console.WriteLine(changeState);

                switch (changeState)
                {
                    case 1:
                        if (timeDelay > finalTimeDelay)
                        {
                            State_AI = "attack";
                            timeDelay = 0;
                            isSet = false;
                        }
                        break;
                    case 2:
                        if (timeDelay > finalTimeDelay)
                        {
                            State_AI = "jump";
                            timeDelay = 0;
                            isSet = false;
                        }
                        break;
                }
            }
            // else, move closer to player
            else
            {
                //Console.WriteLine("no");
                //if player is on the right
                if (StateEnd)
                {
                    if (EnemyAIPos.X < offSet.X)
                        State_AI = "run to right";
                    else
                        State_AI = "run to left";
                }

            }
            //}
        }

        private static Random random = new Random();

        public static float FloatRandomBetween(float min, float max)
        {
            return min + (float)random.NextDouble() * (max - min);
        }

        /*
        public static int IntRandomBetween(int min, int max)
        {
            return min + random.NextDouble() * (max - min);
        }*/
    }
}


/*

            Update()
            Vector2 distance = Vector2.Distance(playerPos, enemyPos);

            if (distance < enemyAttackRange) {
                //send attack animation
            }

            else if (distance > enemyAttackRange) {
                if (enemyPos.X < PlayerPos.X) 
                    enemyPos.X += enemySpeed;
                else 
                    enemyPos.X -= enemySpeed;
            }

            if (PlayerPos.Y <= enemyPos.Y) {
                //send jumping animation
                enemyPos.Y -= enemySpeed;
            }

            Draw()
             if (enemyPos.X < playerPos.X)
                spriteBatch.Draw(EnemyAITexture,enemyPos ,null,Color.White,rotation,EnemyAIOri,1f, SpriteEffects.FlipHorizontally,0f);
            else
                spriteBatch.Draw(EnemyAITexture, enemyPos , null, Color.White, rotation, EnemyAIOri, 1f, SpriteEffects.None, 0f);

*/